﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using zkemkeeper;
using QUANLYCHAMCONG.BL.BUS;
using Hopthoai;
using QUANLYCHAMCONG.BL.Entities;
using Microsoft.VisualBasic.CompilerServices;
using QUANLYCHAMCONG.BL.BUS.Import;
using Microsoft.VisualBasic;
using QUANLYCHAMCONG.BL;

namespace QUANLYCHAMCONG.PL.CMS.Maychamcong
{
    public partial class DownloadNhanVien : UserControl
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        public DownloadNhanVien()
        {
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
            InitializeComponent();
        }
        string mamay = "";
        bool IsConnect;
        MAYCHAMCONG mcc;
        Template temp;
        TemplateCololection dsvantay;
        int flag = 0;
        public CZKEM AxCZKEM1 = new CZKEMClass();
        frmHoidap thongbao;
        string seri = "";
        string serimahoa = "";
        STAFF staff;
        STAFFCollection dsnhanvien;
        public Total UcStatusRoom { get; set; }
        public void ReloadStattus()
        {
            Thongke tk = new Thongke();
            this.UcStatusRoom.DtData = BAOCAOBUS.ThongkeTotal(tk);
            this.UcStatusRoom.Settext();
        }
        void ActiveToolbar()
        {
            this.toolbarMayChamCong1.SetEnableButton(true, false, false, false, false, true, true, true, true);
        }
        private void DownloadNhanVien_Load(object sender, EventArgs e)
        {
            ActiveToolbar();
            LoadAllMAY();
            SetLink();
        }
        private void SetLink()
        {
            this.links1.SetValues("Máy", "Tải nhân viên từ máy chấm công");
        }
        void LoadAllMAY()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = MAYCHAMCONGBUS.SelectAll(null);
                this.toolbarMayChamCong1.DataLoaiMay = dt;
                this.toolbarMayChamCong1.DisplayMember = "TENMAY";
                this.toolbarMayChamCong1.ValuesMember = "MAMCC";
                this.toolbarMayChamCong1.AddDataForCMB();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message.ToString());
            }
        }
        private void toolbarMayChamCong1_ChangeMachine()
        {
            mamay = this.toolbarMayChamCong1.SelectValue;
        }
        private void DataTableToMaychamcong(ref MAYCHAMCONG mcc, DataTable dt)
        {
            if (dt.Rows.Count != 0)
            {

                foreach (DataRow row in dt.Rows)
                {
                    mcc = new MAYCHAMCONG();
                    mcc.COMPORT = int.Parse(row["COMPORT"].ToString());
                    mcc.DOMAIN = row["DOMAIN"].ToString();
                    mcc.FIMWARE = row["FIMWARE"].ToString();
                    mcc.IDMAY = int.Parse(row["IDMAY"].ToString());
                    mcc.IP = row["IP"].ToString();
                    if (row["ISDOMAIN"].ToString() == "USE")
                        mcc.ISDOMAIN = true;
                    else
                        mcc.ISDOMAIN = false;
                    mcc.MALKN = row["MALKN"].ToString();
                    mcc.MALM = row["MALM"].ToString();
                    mcc.MAMCC = row["MAMCC"].ToString();
                    mcc.MATD = row["MATD"].ToString();
                    mcc.PASSWORD = row["PASSWORD"].ToString();
                    mcc.PORT = row["PORT"].ToString();
                    mcc.SeriNumber = row["SeriNumber"].ToString();
                    mcc.SODANGKI = row["SODANGKI"].ToString();
                    mcc.TENMAY = row["TENMAY"].ToString();
                    mcc.TONGSOMATMA = int.Parse(row["TONGSOMATMA"].ToString());
                    mcc.TONGSONHANVIEN = int.Parse(row["TONGSONHANVIEN"].ToString());
                    mcc.TONGSOVANTAY = int.Parse(row["TONGSOVANTAY"].ToString());
                }
            }
            else
            {
                frmHoidap f = new frmHoidap("Không có dữ liệu", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
            }
        }
        private bool Connection(ConnectType type, bool isdomain, MAYCHAMCONG mcc)
        {
            bool IsComplete = false;
            try
            {
                if (type == ConnectType.COM)
                {
                    /// doc toc do may
                    DataTable tocdo = new DataTable();
                    TOCDO td = new TOCDO();
                    td.MATD = mcc.MATD;
                    tocdo = TOCDOBUS.SelectPK(td);
                    int tocdocom = 0;
                    foreach (DataRow rowtocdo in tocdo.Rows)
                    {
                        tocdocom = int.Parse(rowtocdo["TENTOCDO"].ToString());
                        break;
                    }
                    this.IsConnect = AxCZKEM1.Connect_Com(mcc.COMPORT, mcc.IDMAY, tocdocom);
                }
                else if (type == ConnectType.TCP)
                {
                    if (mcc.ISDOMAIN)
                    {
                        this.IsConnect = AxCZKEM1.Connect_Net(mcc.DOMAIN, int.Parse(mcc.PORT));
                    }
                    else
                    {
                        this.IsConnect = AxCZKEM1.Connect_Net(mcc.IP, int.Parse(mcc.PORT));
                    }
                }
                else if (type == ConnectType.USB)
                {
                    this.IsConnect = AxCZKEM1.Connect_USB(mcc.IDMAY);
                }
                IsComplete = true;
            }
            catch (Exception ex)
            {
                IsComplete =false;
            }
            return IsComplete;
        }
        void CreateConection(MAYCHAMCONG mcc)
        {
            bool IsConnectSuss = false;
            if (mcc.MALKN == "COM")
            {
               IsConnectSuss= Connection(ConnectType.COM, mcc.ISDOMAIN, mcc);
            }
            else if (mcc.MALKN == "TCP")
            {
               IsConnectSuss= Connection(ConnectType.TCP, mcc.ISDOMAIN, mcc);
            }
            else if (mcc.MALKN == "USB")
            {
               IsConnectSuss= Connection(ConnectType.USB, mcc.ISDOMAIN, mcc);
            }
            if (!IsConnectSuss)
            {
                CreateConection(mcc);
            }
        }
        bool CheckSeriAl(string serial)
        {
            mahoa.mahoa mh = new mahoa.mahoa();
            serimahoa = mh.mahoadulieu(seri, false, "thanhthuy");
            if (!Common.IsExists(Application.StartupPath + @"\" + serimahoa + ".GDT"))
            {
                ActiveToolbar();
                thongbao = new frmHoidap("Máy không đúng hoặc chưa được đăng ký \n vui lòng kiểm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                thongbao.ShowDialog();
                return false;
            }
            else
            {
                return true;
            }
        }
        //clock ok
        private void toolbarMayChamCong1_Clock()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    thongbao.ShowDialog();
                }
                else
                {
                    DataTable dt = new DataTable();
                    mcc = new MAYCHAMCONG();
                    mcc.MAMCC = mamay;
                    dt = MAYCHAMCONGBUS.SelectPK(mcc);
                    if (dt.Rows.Count != 0)
                    {
                        mcc = new MAYCHAMCONG();
                        DataTableToMaychamcong(ref mcc, dt);
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                        f.Show();
                        Application.DoEvents();
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if (!CheckSeriAl(seri))
                            {
                                f.Close();
                                AxCZKEM1.Disconnect();
                            }
                            else
                            {
                                int num = 0;
                                int num2 = 0;
                                int num3 = 0;
                                int num4 = 0;
                                int num5 = 0;
                                int num6 = 0;
                                if (!AxCZKEM1.GetDeviceTime(mcc.IDMAY, ref num6, ref num3, ref num, ref num2, ref num4, ref num5))
                                {
                                    f.Close();
                                    thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    thongbao.ShowDialog();
                                }
                                else
                                {
                                    f.Close();
                                    DateTime dateValue = Conversions.ToDate(Conversions.ToString(num6) + "-" + Conversions.ToString(num3) + "-" + Conversions.ToString(num) + " " + Conversions.ToString(num2) + ":" + Conversions.ToString(num4) + ":" + Conversions.ToString(num5));
                                    string str = Until.LayThu(DateAndTime.Weekday(dateValue, FirstDayOfWeek.Sunday));
                                    thongbao = new frmHoidap(mcc.IDMAY + ":" + str + ", " + Conversions.ToString(dateValue), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    thongbao.ShowDialog();
                                    AxCZKEM1.Disconnect();
                                }
                            }
                            if (!IsConnect)
                            {
                                f.Close();
                                ActiveToolbar();
                                thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                thongbao.ShowDialog();
                            }
                        }

                    }
                }
            }
        }
        //dong bo thanh cong
        private void toolbarMayChamCong1_Syssnc()
        {
            int dwYear = DateAndTime.Year(DateAndTime.Now);
            int dwMonth = DateAndTime.Month(DateAndTime.Now);
            int dwDay = DateAndTime.Day(DateAndTime.Now);
            int dwHour = DateAndTime.Hour(DateAndTime.Now);
            int dwMinute = DateAndTime.Minute(DateAndTime.Now);
            int dwSecond = DateAndTime.Second(DateAndTime.Now);
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    thongbao.ShowDialog();
                }
                else
                {
                    DataTable dt = new DataTable();
                    mcc = new MAYCHAMCONG();
                    mcc.MAMCC = mamay;
                    dt = MAYCHAMCONGBUS.SelectPK(mcc);
                    if (dt.Rows.Count != 0)
                    {
                        mcc = new MAYCHAMCONG();
                        DataTableToMaychamcong(ref mcc, dt);
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.Show();
                        f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                        Application.DoEvents();
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if (!CheckSeriAl(seri))
                            {
                                f.Close();
                                AxCZKEM1.Disconnect();
                            }
                            
                            else 
                            {
                                if (!AxCZKEM1.SetDeviceTime2(mcc.IDMAY, dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond))
                                {
                                    f.Close();
                                    thongbao = new frmHoidap("Không thể đồng bộ thời gian với " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    thongbao.ShowDialog();
                                }
                                else
                                {
                                    f.Close();
                                    thongbao = new frmHoidap("Đồng bộ thời gian với " + mcc.IDMAY.ToString() + "thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                    thongbao.ShowDialog();
                                }
                            }

                        }
                        if (!IsConnect)
                        {
                            f.Close();
                            f.Dispose();
                            ActiveToolbar();
                            thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            thongbao.ShowDialog();
                        }
                    }
                }
            }
        }
        private void toolbarMayChamCong1_Restart()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    thongbao.ShowDialog();
                }
                else
                {
                    DataTable dt = new DataTable();
                    mcc = new MAYCHAMCONG();
                    mcc.MAMCC = mamay;
                    dt = MAYCHAMCONGBUS.SelectPK(mcc);
                    if (dt.Rows.Count != 0)
                    {
                        mcc = new MAYCHAMCONG();
                        DataTableToMaychamcong(ref mcc, dt);
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.Show();
                        f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                        Application.DoEvents();
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if (!CheckSeriAl(seri))
                            {
                                f.Close();
                                AxCZKEM1.Disconnect();
                            }
                            else
                            {
                                if (AxCZKEM1.RestartDevice(mcc.IDMAY))
                                {
                                    f.Close();
                                    AxCZKEM1.Disconnect();
                                    thongbao = new frmHoidap("Không thể khởi động lại máy " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    thongbao.ShowDialog();
                                   
                                }
                                else
                                {
                                    f.Close();
                                    AxCZKEM1.Disconnect();
                                    thongbao = new frmHoidap(mcc.IDMAY + "Khởi động lại không thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    thongbao.ShowDialog();
                                    AxCZKEM1.Disconnect();
                                }
                            }
                        }
                        if (!IsConnect)
                        {
                            f.Close();
                            AxCZKEM1.Disconnect();
                            ActiveToolbar();
                            thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            thongbao.ShowDialog();
                        }
                    }
                }
            }
        }
        void CreateTable(ref DataTable dt)
        {
            //tao cau truc bang 
            DataColumn STT = new DataColumn("STT", typeof(string));
            DataColumn MAMCC = new DataColumn("MAMCC", typeof(string));
            DataColumn IDMAY = new DataColumn("IDMAY", typeof(string));
            DataColumn MACCMs = new DataColumn("UserCode", typeof(string));
            DataColumn TENCCs = new DataColumn("TENCC", typeof(string));
            DataColumn IDCardNos = new DataColumn("IDCardNo", typeof(string));
            DataColumn UserPWs = new DataColumn("UserPW", typeof(string));
            DataColumn UserPrivileges = new DataColumn("UserPrivilege", typeof(string));
            DataColumn UserEnableds = new DataColumn("UserEnabled", typeof(string));
            dt.Columns.AddRange(new DataColumn[] { STT, MAMCC, IDMAY, MACCMs, TENCCs, IDCardNos, UserPWs, UserPrivileges, UserEnableds });
            //end tao cau truc bang 
        }
        private void toolbarMayChamCong1_Download()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    thongbao = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    thongbao.ShowDialog();
                }
                else
                {
                    flag = 1;
                    // khai bao bien 
                    int dwValue = 0;
                    this.toolbarMayChamCong1.SetEnableButton(false, false, false, false, true, false, false, false, true);
                    DataTable dt = new DataTable();
                    mcc = new MAYCHAMCONG();
                    mcc.MAMCC = mamay;
                    dt = MAYCHAMCONGBUS.SelectPK(mcc);
                    if (dt.Rows.Count != 0)
                    {
                        mcc = new MAYCHAMCONG();
                        DataTableToMaychamcong(ref mcc, dt);
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.Show();
                        f.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                        Application.DoEvents();
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if (!CheckSeriAl(seri))
                            {
                                f.Close();
                                AxCZKEM1.Disconnect();
                            }
                            else 
                            {
                                int num = 1;
                                DataTable dtforuser = new DataTable();
                                CreateTable(ref dtforuser);
                                DataRow drow;
                                // doing something
                                if (AxCZKEM1.IsTFTMachine(mcc.IDMAY))//neu la may tft
                                {
                                    AxCZKEM1.GetDeviceStatus(mcc.IDMAY, 2, ref dwValue);
                                    f.lbgoi.Text = "Đang đọc thông tin nhân viên của máy " + mcc.IDMAY.ToString();
                                    Application.DoEvents();
                                    if (AxCZKEM1.ReadAllUserID(mcc.IDMAY))
                                    {
                                        f.tocdo.Visible = true;
                                        f.tocdo.Maximum = dwValue;
                                        f.lbgoi.Text = "0%";
                                        Application.DoEvents();
                                        //-----------------------------------------------------------------------------------
                                        //khai bao bien nhan du lieu
                                        string MACCM = "";
                                        string TENCC = "";
                                        string UserPW = "";
                                        int UserPrivilege = 0;
                                        bool UserEnabled = true;
                                        string IDCardNo = "";
                                        int i = 1;
                                        for (int j = 0; j < dwValue; j++)
                                        {
                                            AxCZKEM1.SSR_GetAllUserInfo(mcc.IDMAY, out MACCM, out TENCC, out UserPW, out UserPrivilege, out UserEnabled);
                                            f.tocdo.Value = num;
                                            f.lbgoi.Text = Conversions.ToString((double)(Math.Round((double)(((double)num) / ((double)dwValue)), 2) * 100.0)) + "%"; ;
                                            Application.DoEvents();
                                            //nap du lieu
                                            drow = dtforuser.NewRow();
                                            drow["STT"] = i;
                                            drow["IDMAY"] = mcc.IDMAY;
                                            drow["MAMCC"] = mcc.MAMCC;
                                            drow["UserCode"] = MACCM;
                                            drow["TENCC"] = TENCC;
                                            if (AxCZKEM1.GetStrCardNumber(out IDCardNo))
                                            {
                                                drow["IDCardNo"] = IDCardNo;
                                            }
                                            else
                                            {
                                                drow["IDCardNo"] = "0000000000";
                                            }
                                            drow["UserPW"] = UserPW;
                                            drow["UserPrivilege"] = UserPrivilege;
                                            drow["UserEnabled"] = UserEnabled;
                                            dtforuser.Rows.Add(drow);
                                            num++;
                                            i++;
                                        }
                                        //doc song du lieu 
                                        dgvnhanvien.DataSource = dtforuser;
                                        f.Close();
                                    }
                                    else
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap("Máy " + mcc.IDMAY.ToString() + "Chưa có dữ liệu nhân viên!!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                        AxCZKEM1.Disconnect();
                                        ActiveToolbar();
                                    }
                                }
                                else 
                                {
                                    AxCZKEM1.GetDeviceStatus(mcc.IDMAY, 2, ref dwValue);
                                    f.lbgoi.Text = "Đang đọc thông tin nhân viên của máy " + mcc.IDMAY.ToString();
                                    Application.DoEvents();
                                    if (AxCZKEM1.ReadAllUserID(mcc.IDMAY))
                                    {
                                        f.tocdo.Visible = true;
                                        f.tocdo.Maximum = dwValue;
                                        f.lbgoi.Text = "0%";
                                        Application.DoEvents();
                                        //-----------------------------------------------------------------------------------
                                        //khai bao bien nhan du lieu
                                        int VMACCM = 0;
                                        string TENCC = "";
                                        String UserPW = "";
                                        int UserPrivilege = 0;
                                        bool UserEnabled = true;
                                        string IDCardNo = "";
                                        int i = 1;
                                        for (int j = 0; j < dwValue; j++)
                                        {
                                            AxCZKEM1.GetAllUserInfo(mcc.IDMAY, ref VMACCM, ref TENCC, ref UserPW, ref UserPrivilege, ref UserEnabled);
                                            f.tocdo.Value = num;
                                            f.lbgoi.Text = Conversions.ToString((double)(Math.Round((double)(((double)num) / ((double)dwValue)), 2) * 100.0)) + "%"; ;
                                            Application.DoEvents();
                                            //nap du lieu
                                            drow = dtforuser.NewRow();
                                            drow["STT"] = i;
                                            drow["IDMAY"] = mcc.IDMAY;
                                            drow["MAMCC"] = mcc.MAMCC;
                                            drow["UserCode"] = VMACCM.ToString();
                                            drow["TENCC"] = TENCC;
                                            if (AxCZKEM1.GetStrCardNumber(out IDCardNo))
                                            {
                                                drow["IDCardNo"] = IDCardNo;
                                            }
                                            else
                                            {
                                                drow["IDCardNo"] = "0000000000";
                                            }
                                            drow["UserPW"] = UserPW;
                                            drow["UserPrivilege"] = UserPrivilege;
                                            drow["UserEnabled"] = UserEnabled;
                                            dtforuser.Rows.Add(drow);
                                            num++;
                                            i++;
                                        }
                                        //doc song du lieu 
                                        dgvnhanvien.DataSource = dtforuser;
                                        f.Close();
                                    }
                                    else
                                    {
                                        f.Close();
                                        thongbao = new frmHoidap("Máy " + mcc.IDMAY.ToString() + "Chưa có dữ liệu nhân viên!!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        thongbao.ShowDialog();
                                        AxCZKEM1.Disconnect();
                                        ActiveToolbar();
                                    }
                                }
                            }

                        }
                        if (!IsConnect)
                        {
                            f.Close();
                            f.Dispose();
                            ActiveToolbar();
                            thongbao = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + "vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            thongbao.ShowDialog();
                        }

                    }
                }
            }

        }
        private void UpDateTongNVMayChamCong(int  tongnhanvien, MAYCHAMCONG cc)
        {
            mcc = new MAYCHAMCONG();
            mcc.COMPORT = cc.COMPORT;
            mcc.DOMAIN = cc.DOMAIN;
            mcc.FIMWARE = cc.FIMWARE;
            mcc.IDMAY = cc.IDMAY;
            mcc.IP = cc.IP;
            mcc.ISDOMAIN = cc.ISDOMAIN;
            mcc.MALKN = cc.MALKN;
            mcc.MALM = cc.MALM;
            mcc.MAMCC = cc.MAMCC;
            mcc.MATD = cc.MATD;
            mcc.PASSWORD = cc.PASSWORD;
            mcc.PORT = cc.PORT;
            mcc.SeriNumber = mcc.SeriNumber;
            mcc.SODANGKI = cc.SODANGKI;
            mcc.TENMAY = cc.TENMAY;
            mcc.TONGSOMATMA = cc.TONGSOMATMA;
            mcc.TONGSONHANVIEN = tongnhanvien;
            mcc.TONGSOVANTAY = cc.TONGSOVANTAY;
            MAYCHAMCONGBUS.Update(mcc);
        }
        private void toolbarMayChamCong1_Save()
        {
            try
            {
                if (flag == 1 && dgvnhanvien.RowCount != 0)//download song thi luu
                {
                    bool IsComplete = true;
                    dsnhanvien = new STAFFCollection();
                    dsvantay = new TemplateCololection();
                    foreach (DataGridViewRow rows in dgvnhanvien.Rows)
                    {
                        staff = new STAFF();
                        temp = new Template();
                        staff.MACCM = rows.Cells["UserCode"].Value.ToString();
                        staff.TENCC = rows.Cells["TENCC"].Value.ToString();
                        staff.UserPW = rows.Cells["UserPW"].Value.ToString();
                        staff.UserCardNo = rows.Cells["IDCardNo"].Value.ToString();
                        staff.UserPrivilege = int.Parse(rows.Cells["UserPrivilege"].Value.ToString());
                        staff.UserEnabled = bool.Parse(rows.Cells["UserEnabled"].Value.ToString());
                        dsnhanvien.Add(staff);
                    }
                    //sau khi them danh sach thanh cong
                    //them vao co so du lieu
                    foreach (STAFF nv in dsnhanvien)
                    {
                        IsComplete = STAFFBUS.Insert(true, nv);
                    }

                    if (IsComplete)
                    {
                        thongbao = new frmHoidap("Cập nhật thông tin Nhân viên thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        thongbao.ShowDialog();
                        this.toolbarMayChamCong1.SetEnableButton(true, false, false, false, false, true, true, true, true);
                    }
                    else
                    {
                        thongbao = new frmHoidap("Cập nhật thông tin Nhân viên thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        thongbao.ShowDialog();
                        this.toolbarMayChamCong1.SetEnableButton(true, false, false, false, false, true, true, true, true);
                    }
                }
            }
            catch (Exception ex)
            {
                thongbao = new frmHoidap("Lỗi:"+ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                thongbao.ShowDialog();
            }
            finally { ReloadStattus(); }
        }
        public delegate void _Close();
        public event _Close Close;
        private void toolbarMayChamCong1_Exit()
        {
            if (this.Close != null)
                this.Close();
        }
        
    }
}
